fix(service): 补齐 compact keepalive writer nil 守卫#4019
Open
wucm667 wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
生产真实流量下
/v1/responses(多为 stream)在请求结束后会反复出现 recovered panic:runtime error: invalid memory address or nil pointer dereference。关键路径是 Logger 中间件在请求结束后读取c.Writer.Status()/Written(),委托链进入openAICompactKeepaliveWriter后访问已释放的内层ResponseWriter,导致空指针 panic。Fixes #4000
根因
openAICompactKeepaliveWriter的读侧委托方法Status/Size/Written直接在锁内调用w.ResponseWriter.Xxx(),没有处理请求结束后内层 writer 被释放为 nil 的场景;写侧委托方法也缺少相同的防御性 nil 守卫。#3994 /
bc3cb290已经给兄弟包装器opsCaptureWriter补齐了委托方法 nil 守卫,但没有覆盖service/openai_compact_sse_keepalive.go这一层,因此 keepalive writer 仍会裸调用内层 writer。改动
openAICompactKeepaliveWriter的Header、Write、WriteString、WriteHeader、WriteHeaderNow、Flush、Status、Size、Written补齐 nil 守卫。gin.ResponseWriter暴露的Hijack、CloseNotify、Pusher补齐与 fix(handler): opsCaptureWriter 释放后 nil pointer panic #3994 一致的防御性实现。suspend()增加w.k == nil守卫,避免防御场景下停拍时空指针。验证